package com.jinzhi.jinzhidata.repository;

import com.jinzhi.jinzhidata.vo.ListResourceVO;
import com.jinzhi.jinzhidata.dataobject.Resource;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @description
 * @date 2019-12-29 13:13
 **/
@Repository
public interface ResourceRepository extends JpaRepository<Resource, String> {

    @Query("select " +
            "new com.jinzhi.jinzhidata.vo.ListResourceVO( " +
            "r.resourceId,r.resourceCover,r.resourceName,r.resourceIntroduction," +
            "r.resourcePrice,u.userName,r.resourceState,r.createTime,r.updateTime) " +
            "from Resource r, User u where r.resourceAuthorId = u.userId and r.resourceState = :resourceState order by r.createTime desc ")
    List<ListResourceVO> selectResourceList(String resourceState);

    @Query("select " +
            "new com.jinzhi.jinzhidata.vo.ListResourceVO( " +
            "r.resourceId,r.resourceCover,r.resourceName,r.resourceIntroduction," +
            "r.resourcePrice,u.userName,r.resourceState,r.createTime,r.updateTime) " +
            "from Resource r, User u where r.resourceAuthorId = u.userId and u.userId = :userId order by r.createTime desc ")
    List<ListResourceVO> selectResourceListByAuthor(String userId);


    @Query("select " +
            "new com.jinzhi.jinzhidata.vo.ListResourceVO( " +
            "r.resourceId,r.resourceCover,r.resourceName,r.resourceIntroduction," +
            "r.resourcePrice,u.userName,r.resourceState,r.createTime,r.updateTime) " +
            "from Resource r, User u where r.resourceAuthorId = u.userId " +
            "and r.resourceId in :resourceIdList order by r.createTime desc ")
    List<ListResourceVO> selectUserBuyResourceList(List<String> resourceIdList);

    @Query("select " +
            "new com.jinzhi.jinzhidata.vo.ListResourceVO( " +
            "r.resourceId,r.resourceCover,r.resourceName,r.resourceIntroduction," +
            "r.resourcePrice,u.userName,r.resourceState,r.createTime,r.updateTime) " +
            "from Resource r, User u,ResourceType rt " +
            "where r.resourceAuthorId = u.userId " +
            "and r.resourceTypeId = rt.resourceTypeId " +
            "and rt.resourceTypeName = :typeName and r.resourceState = :resourceState order by r.createTime desc ")
    List<ListResourceVO> selectResourceListByType(String resourceState, String typeName, Pageable pageable);

    @Query("select " +
            "new com.jinzhi.jinzhidata.vo.ListResourceVO( " +
            "r.resourceId,r.resourceCover,r.resourceName,r.resourceIntroduction," +
            "r.resourcePrice,u.userName,r.resourceState,r.createTime,r.updateTime) " +
            "from Resource r, User u,ResourceType rt " +
            "where r.resourceAuthorId = u.userId " +
            "and r.resourceTypeId = rt.resourceTypeId " +
            "and r.resourceName like CONCAT('%',:resourceName,'%') " +
            "and r.resourceState = :resourceState order by r.createTime desc ")
    List<ListResourceVO> searchResourceByName(String resourceState, String resourceName, Pageable pageable);

    @Query("select " +
            "new com.jinzhi.jinzhidata.vo.ListResourceVO( " +
            "r.resourceId,r.resourceCover,r.resourceName,r.resourceIntroduction," +
            "r.resourcePrice,u.userName,r.resourceState,r.createTime,r.updateTime) " +
            "from Resource r, User u where r.resourceAuthorId = u.userId order by r.createTime desc ")
    List<ListResourceVO> selectResourceList();
}
